﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kendo.Mvc.UI;
using NorthwindSample.Data;

namespace NorthwindSample.Controllers
{
    public class ShippersController : Controller
    {
        //
        // GET: /Shippers/

        public ActionResult Index()
        {
            return View();
        }


        public ActionResult GetShippers([DataSourceRequest] DataSourceRequest request)
        {
            var shippers = ShippersRepository.GetShippers().Select(e => new
            {
                ShipperID = e.ShipperID,
                CompanyName = e.CompanyName,
                Phone = e.Phone
            });

            return Json(new DataSourceResult { Data = shippers });
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CreateShipper([DataSourceRequest] DataSourceRequest request, Shipper shipper)
        {
            ShippersRepository.SaveShipper(shipper);
            return Json(new DataSourceResult { Data = ModelState }, JsonRequestBehavior.AllowGet);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UpdateShipper([DataSourceRequest] DataSourceRequest request, Shipper shipper)
        {
            ShippersRepository.SaveShipper(shipper);
            return Json(new DataSourceResult { Data = ModelState }, JsonRequestBehavior.AllowGet);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult DestroyShipper([DataSourceRequest] DataSourceRequest request, Shipper shipper)
        {
            ShippersRepository.DeleteShipper(shipper.ShipperID);
            return Json(new DataSourceResult { Data = ModelState }, JsonRequestBehavior.AllowGet);
        }
    }
}
